﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterWay.Model.RHDB;
using WaterWay.Model.SYS;
using WaterWay.Utilities;
using WaterWay.WeChat.Models;
using WaterWayLib;

namespace WaterWay.WeChat.Controllers
{
    /// <summary>
    /// 实时水情
    /// </summary>
    public class WaterController : Controller
    {
        //
        // GET: /Water/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Detail()
        {
            return View();
        }
        /// <summary>
        /// 获取站点基本信息
        /// </summary>
        /// <param name="onlySuper">控制是否仅显示超警</param>
        /// <returns></returns>
        public ActionResult GetWaterStationInfo(string onlySuper)
        {

            SYSService.SYSServiceClient client = new SYSService.SYSServiceClient();
            RHDBService.RHDBServiceClient rhdbClient = new RHDBService.RHDBServiceClient();
            var waterInfoList = new List<WaterStationInfo>();
            var result = client.WX_Get_Stations("", 1503);
            if (result.SourceData.Length > 0)
            {
                List<BDMS_SYS_STATION> stationList = result.SourceData.Decodes<BDMS_SYS_STATION>();
                for (int i = 0; i < stationList.Count; i++)
                {

                    string stcd = stationList[i].STCD;
                    ST_TIDE_R tide = rhdbClient.WX_QueryTide("", 1503, stcd).SourceData.Decode<ST_TIDE_R>();
                    WaterStationInfo wsInfo = new WaterStationInfo
                    {
                        STCD = stcd,//站码
                        STNM = stationList[i].STNM,//站名
                        TIME = tide.TM,//时间
                        WATER = tide.TDZ ?? 0,//水位
                        SUPER = tide.TDZ ?? 0 - Convert.ToDouble(stationList[i].PROPVAL ?? "0"),//超警(水位减警戒线)
                        WARNING = stationList[i].PROPVAL??"0",//警戒线
                        //ISSUPER = Convert.ToDouble(stationList[i].PROPVAL)>(tide.TDZ??0)  //是否超警

                    };
                    waterInfoList.Add(wsInfo);
                }

            }
            //判断是否只显示超警
            if (!string.IsNullOrEmpty(onlySuper))
            {
                waterInfoList = waterInfoList.Where(n => n.SUPER > 0).ToList();
            }
            return Content(waterInfoList.ToJsonDateFull());
        }
    }
}
